/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.umum.msi;

/**
 *
 * @author Administrator
 */
import cn.umum.msi.listener.MSIAdapter;
/**
 *
 * @author Administrator
 */
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.ui.dwr.Util;
import org.directwebremoting.ui.browser.Window;
import org.directwebremoting.ui.Callback;

public class JavascriptMSI implements Runnable {

    /**
     * Create a schedule to update the clock every second.
     */
    public JavascriptMSI() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
    }

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {

        //if (isactived) {
        count = MSIAdapter.getCount();

        setClockDisplay(new Date().toString());

    //} else {
    //setClockDisplay("msi stopped,please connect at once");
    //}
    }

    /**
     * Called from the client to turn the clock on/off
     */
    public synchronized void toggle() {
        isactived = !isactived;

        if (isactived) {
            setClockDisplay("Started");
        }
    //msiStart();
    }

    /**
     * Actually alter the clients.
     * In DWR 2.x you had to know the ServletContext in order to be able to get
     * a ServerContext. With DWR 3.0 this restriction has been removed.
     * This method is public so you can call this from the dwr auto-generated
     * pages to demo altering one page from another
     * @param output The string to display.
     */
    public void setClockDisplay(final String output) {
        String page = ServerContextFactory.get().getContextPath() + "/msi/userinfo";
        Browser.withPage(page, new Runnable() {

            public void run() {
                //Util.setValue("clockDisplay", new Date().toString());
                //Callback callback =null;
                //Window.confirm(output, callback);
                //Window.alert(callback.toString());
                ScriptSessions.addFunctionCall("setTime", new Date().toString());
                ScriptSessions.addFunctionCall("userStatEvent", MSIAdapter.agentStats.values());
                //ScriptSessions.addFunctionCall("userStatEvent", MSIAdapter.agentStatsChange.values());
                //Util.removeAllOptions("users");
                //Util.addOptions("users", MSIAdapter.agentStatsChange, "agentStatsChange");
                //Util.addOptions("users", MSIAdapter.agentStats.values(), "userid");
                //Window.alert("No call found");

                ScriptSessions.addFunctionCall("changeStatCurrent");
            }
        });
    }

    /**
     * Are we updating the clocks on all the pages?
     */
    public String deleteUser(String id) {
        User u = MSIAdapter.agentStatsChange.remove(id);
        if (u == null) {
            return "User does not exist";
        } else {
            return "Deleted " + u.getUsername();
        }
    }

    public void addUser(String id, String agentDN, int UserStat) {
        //User u = MSIAdapter.agentStats.remove(id);
        User user;
        user = new User();
        user.setUserid(id);
        user.setUsername("username" + id);
        user.setagentDN(agentDN);
        user.setUserStat(UserStat);
        MSIAdapter.agentStats.put(id, user);
    //return "Deleted " + u.getUsername();
    // }
    }

    public Collection<User> getUserStatsChange() {
        return MSIAdapter.agentStats.values();
    }
  
    protected transient boolean isactived = false;
    private int count;
    //public static List<User> users = MSIAdapter.users;
}
